package com.sc.study.foundation.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sc.study.foundation.mapper.ScSubjectChapterMapper;
import com.sc.study.foundation.mapper.ScSubjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sc.study.foundation.mapper.ScSubjectChapterMapper;
import com.sc.study.foundation.domain.ScSubjectChapter;
import com.sc.study.foundation.service.IScSubjectChapterService;

/**
 * 课程章节Service业务层处理
 *
 * @author sc
 * @date 2022-03-05
 */
@Service
public class ScSubjectChapterServiceImpl implements IScSubjectChapterService {
    @Autowired
    private ScSubjectChapterMapper subjectChapterMapper;
    @Autowired
    private ScSubjectMapper scSubjectMapper;

    @Override
    public ScSubjectChapter selectSubjectChapterBySubId(Integer subjectId, Integer subjectTime,Integer num) {
        return subjectChapterMapper.selectSubjectChapterBySubId(subjectId,subjectTime,num);
    }

    /**
     * 查询课程章节
     *
     * @param id 课程章节主键
     * @return 课程章节
     */
    @Override
    public ScSubjectChapter selectSubjectChapterById(Long id) {
        return subjectChapterMapper.selectSubjectChapterById(id);
    }

    /**
     * 查询课程章节列表
     *
     * @param subjectChapter 课程章节
     * @return 课程章节
     */
    @Override
    public List<ScSubjectChapter> selectSubjectChapterList(ScSubjectChapter subjectChapter) {
        List<ScSubjectChapter> scSubjectChapterList = subjectChapterMapper.selectSubjectChapterList(subjectChapter);
        scSubjectChapterList.forEach(item -> {
            item.setScSubject(scSubjectMapper.selectScSubjectBySubid(item.getSubjectid()));
        });
        return scSubjectChapterList;
    }

    /**
     * 新增课程章节
     *
     * @param subjectChapter 课程章节
     * @return 结果
     */
    @Override
    public int insertSubjectChapter(ScSubjectChapter subjectChapter) {
        ScSubjectChapter scSubjectChapter=new ScSubjectChapter();
        scSubjectChapter.setSubjectid(subjectChapter.getSubjectid());
        scSubjectChapter.setOrder(subjectChapter.getOrder());
        List<ScSubjectChapter> scSubjectChapterList = subjectChapterMapper.selectSubjectChapterList(scSubjectChapter);
        if(scSubjectChapterList.size()>0){
            return 0;
        }
        return subjectChapterMapper.insertSubjectChapter(subjectChapter);
    }

    /**
     * 修改课程章节
     *
     * @param subjectChapter 课程章节
     * @return 结果
     */
    @Override
    public int updateSubjectChapter(ScSubjectChapter subjectChapter) {
        return subjectChapterMapper.updateSubjectChapter(subjectChapter);
    }

    /**
     * 批量删除课程章节
     *
     * @param ids 需要删除的课程章节主键
     * @return 结果
     */
    @Override
    public int deleteSubjectChapterByIds(Long[] ids) {
        return subjectChapterMapper.deleteSubjectChapterByIds(ids);
    }

    /**
     * 删除课程章节信息
     *
     * @param id 课程章节主键
     * @return 结果
     */
    @Override
    public int deleteSubjectChapterById(Long id) {
        return subjectChapterMapper.deleteSubjectChapterById(id);
    }

    /**
     * 返回Map集合： key为id value为subjectId
     * @return
     */
    @Override
    public Map<Long, Long> selectSubjectChapterMap() {
        Map<Long, Long> returnMap = new HashMap<>();
        List<Map<Object, Object>> mapList = subjectChapterMapper.selectSubjectChapterMap();

        for (Map<Object, Object> map: mapList) {
            Long id = null;
            Long subjectId = null;
            for (Map.Entry<Object, Object> maps:map.entrySet()) {
                if("id".equals(maps.getKey().toString())){
                    id = Long.valueOf(Integer.valueOf(maps.getValue().toString()));
                }
                if("subjectId".equals(maps.getKey().toString())){
                    subjectId = Long.valueOf(maps.getValue().toString());
                }
                returnMap.put(id, subjectId);
            }
        }
        return returnMap ;
    }
}
